<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span id="flowsetname"></span><div class="move_title"></div><?php include "../public/subturn.php";?></div></div>
    <div class="main-container">


        <div class="block-generic">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">申请人</label>
                        <div class="layui-input-block">
                            <input type="text" id="flowuser" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">申请部门</label>
                        <div class="layui-input-block">
                            <input type="text" id="flowdept" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">协议类型</label>
                        <div class="layui-input-block radio-block">
                            <input type="radio" name="neihetype" lay-filter="neihetype" checked value="1" title="年度合作协议">
                            <input type="radio" name="neihetype" lay-filter="neihetype" value="2" title="单项目合作协议">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">协议编号</label>
                        <div class="layui-input-block">
                            <input type="text" id="number" readonly placeholder="请输入" clearable autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>协议名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" id="name" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>协议年份</label>
                        <div class="layui-input-block">
                            <select id="year" name="year" lay-filter="year" ></select>
                            <p class="error" id="year_error" style="margin: 0;"></p>
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>内包责任人</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="dutyer" class="layui-input dutyer" onclick="openPage('../public/choose_dealcom.php?dom=dutyer&page=contract_nei','80%','80%')">
                            <div class="choose_div" id="dutyer" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_dealcom.php?dom=dutyer&page=contract_nei','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">联系方式</label>
                        <div class="layui-input-block">
                            <input type="text" name="dutytel" id="dutytel" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>所属单位</label>
                        <div class="layui-input-block">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=chanid&type=dept&dept=chan&single=1','770px','490px');"></div>
                                <div class="tanxiannei" id="chanid"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=chanid&type=dept&dept=chan&single=1','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                            <div id="chanid_tanerror" class="tanerrormsg"></div>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">渠道来源</label>
                        <div class="layui-input-block">
                            <input type="text" name="canal" id="canal" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>





            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>合同状态</label>
                        <div class="layui-input-block">
                            <select id="state" name="state" lay-filter="state" ></select>
                            <p class="error" id="state_error" style="margin: 0;"></p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">签订日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="qiandt" id="qiandt" readonly placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">经办人</label>
                        <div class="layui-input-block">
                            <input type="text" name="baner" id="baner" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">结算方式</label>
                        <div class="layui-input-block">
                            <textarea id="settle" name="settle" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">关联单据</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="flowrelas" class="layui-input flowrelas" onclick="openPage('../public/choose_flowrelas.php?dom=flowrelas','80%','80%');">
                            <div class="choose_div" id="flowrelas" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_flowrelas.php?dom=flowrelas','80%','80%');"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-hide" id="advanersDiv">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">预审人</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=advaners&type=user&single=2','770px','490px');"></div>
                                <div class="tanxiannei" id="advaners"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=advaners&type=user&single=2','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-hide" id="nexterDiv">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>指定下一步审核人</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=nexter&type=user&single=1','770px','490px');"></div>
                                <div class="tanxiannei" id="nexter"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=nexter&type=user&single=1','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include '../public/upinfo.php'?>
        </div>

        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>费用设置</span><span id="kouhelpss" style="cursor: pointer;font-size: 18px"><i class="layui-icon layui-icon-help"></i></span></div>
                        <div>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_copy_row">复制行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th><text>*</text>费用项</th>
                            <th><text>*</text>计算方式</th>
                            <th><text>*</text>计算值</th>
                            <th>开始计算月份</th>
                            <th>结束计算月份</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>
    </div>
</form>
<script>
    $("#commentForm").validate({
        rules: {
            name: {required: true},
        },
        messages:{
            name: {required: "请输入"},
        }
    });

    var pub_feiname;
    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();



        facade.ajax({type: 'get', url: ask_flowdata_add_contract_nei}).done(function(res){
            if (res.code === 2000) {
                var data = res.data;
                $("#flowsetname").text(data.flowsetname);
                $("#flowuser").val(data.flowuser);
                $("#flowdept").val(data.flowdept);
                $("#number").val(data.number);
                if(data.isadvan){
                    $("#advanersDiv").removeClass('layui-hide');
                }
                if(data.isnext){
                    $("#nexterDiv").removeClass('layui-hide');
                }

                var year_str = '<option value="">请选择</option>';
                var year = res.data.year;
                for (var key in year){
                    year_str += '<option value="'+year[key]+'">'+year[key]+'</option>';
                }
                $("#year").html(year_str);

                var state_str = '<option value="">请选择</option>';
                var state = res.data.state;
                for (var key in state){
                    state_str += '<option value="'+key+'">'+state[key]+'</option>';
                }
                $("#state").html(state_str);



                pub_feiname = res.data.feiname;
                var feitype = res.data.feitype;

                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td>';
                listAddStr += '<div class="feiname"></div>';
                listAddStr += '</td>';
                listAddStr += '<td>';
                listAddStr += '<select class="layui-select feitype" lay-filter="feitype">';
                listAddStr += '<option value="">请选择</option>';
                for (var key in feitype) {
                    listAddStr += '<option value="'+key+'">'+feitype[key]+'</option>';
                }
                listAddStr += '</select>';
                listAddStr += '</td>';
                listAddStr += '<td><input type="number" class="layui-input feival"></td>';
                listAddStr += '<td><input type="text" class="layui-input startdt " readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input enddt" readonly></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr, 1, {name:'feiname', data:res.data.feiname, value:''});




                public_upload_file_load('upload_files');
                render_from();
                render_date();
                initParseMoney();
            }
        });



        form.on('select(feitype)', function(data){
            if(data.value == 2){
                $(this).parents('tr').find('.startdt').addClass('bdate_ym');
                $(this).parents('tr').find('.enddt').addClass('bdate_ym');
            }else{
                $(this).parents('tr').find('.startdt').parent('td').html('<input type="text" class="layui-input startdt " readonly>');
                $(this).parents('tr').find('.enddt').parent('td').html('<input type="text" class="layui-input enddt " readonly>');
            }
            render_from();
            render_date();
        });


        form.on('select(year)', function(data){
            if(data.value == ''){
                $('#year_error').show().text('请选择');
                $('#year_error').parent().find('input').addClass('select_error');
            }else{
                $('#year_error').hide();
                $('#year_error').parent().find('input').removeClass('select_error');
            }
        });

        form.on('select(state)', function(data){
            if(data.value == ''){
                $('#state_error').show().text('请选择');
                $('#state_error').parent().find('input').addClass('select_error');
            }else{
                $('#state_error').hide();
                $('#state_error').parent().find('input').removeClass('select_error');
            }
        });

        form.on('submit(add)', function(data){
            data.field.isturn = $(this).attr('key') || 0;
            data.field.flowrelas = get_choose_val('flowrelas');
            data.field.advaners = tan_tags_getvalue("#advaners", 'user');
            data.field.nexter = tan_tags_getvalue("#nexter", 'user');
            data.field.chanid = tan_tags_getvalue("#chanid", 'dept');
            data.field.dutyer = get_choose_val('dutyer');

            if(data.field.isturn > 0){
                var validRes = $("#commentForm").valid();

                if(data.field.year == ''){
                    $('#year_error').show().text('请选择');
                    $('#year_error').parent().find('input').addClass('select_error');
                    validRes = false;
                }else{
                    $('#year_error').hide();
                    $('#year_error').parent().find('input').removeClass('select_error');
                }

                formValid('dutyer');
                if(data.field.dutyer == undefined || !data.field.dutyer){
                    validRes = false;
                }

                if(!data.field.chanid){
                    $("#chanid").parents('.tanxian').addClass('tanerror');
                    $('#chanid_tanerror').text('请选择');
                    validRes = false;
                }else{
                    $("#chanid").parents('.tanxian').removeClass('tanerror');
                    $('#chanid_tanerror').text('');
                }

                if(data.field.state == ''){
                    $('#state_error').show().text('请选择');
                    $('#state_error').parent().find('input').addClass('select_error');
                    validRes = false;
                }else{
                    $('#state_error').hide();
                    $('#state_error').parent().find('input').removeClass('select_error');
                }

                if(!validRes){return false;}
            }


            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var feiname = $(this).find('.feiname').find('input').val();
                    var feitype = $(this).find('.feitype').val();
                    var feival = $(this).find('.feival').val();
                    var startdt = $(this).find('.startdt').val();
                    var enddt = $(this).find('.enddt').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(data.field.isturn > 0){
                        if(!feiname){
                            throw new Error('第'+index+'行，请选择费用项');
                        }
                        if(!feitype){
                            throw new Error('第'+index+'行，请选择计算方式');
                        }
                        if(!feitype){
                            throw new Error('第'+index+'行，请输入计算值');
                        }
                        if(feitype == 2){
                            if(!startdt){
                                throw new Error('第'+index+'行，请选择开始计算月份');
                            }
                        }else{
                            startdt = '';
                            enddt = '';
                        }
                    }


                    list.push({
                        feiname:feiname,
                        feitype:feitype,
                        feival:feival,
                        startdt:startdt,
                        enddt:enddt,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;


            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);
            public_flow_submit(data.field, ask_flowdata_add_contract_nei, ask_flowdata_edit_contract_nei, $(this).attr('but'));
            return false;
        });

    });


    function choose_dutyer_delete() {
        $("#dutytel").val('');
    }




    var curTipIndex = '';
    $("#kouhelpss").on("mouseenter", function () {
        var content = '<div style="padding: 5px; font-size: 12px; color: #FFFFFF;">' +
            '<div>·按固定金额：填写的「计算值」将直接作为该费项的应收取值</div>'+
            '<div>·按月固定金额：填写的「计算值」将每月进行累加（需填写开始和结束月份）</div>'+
            '<div>·按合同金额比例：填写的「计算值」将作为计算比例，与施工合同中的「合同金额」的金额相乘得出应收</div>'+
            '<div>·按工程收入比例：填写的「计算值」将作为计算比例，与收款登记中的「工程款」的金额相乘得出应收</div>'+
            '<div>·按开票金额比例：填写的「计算值」将作为计算比例，与开票登记中的的含税金额相乘得出应收</div>'+
            '<div>·按用证清单比例：适用于证书费，填写的「计算值」将作为计算比例，与用证清单的使用费合计金额相乘得出应收</div>'+
            "</div>";
        curTipIndex = layer.tips(content,this, {time: -1, maxWidth: 600, tips: [3, "#2272eb"],});
    }).on("mouseleave", function () {
        layer.close(curTipIndex);
    });
</script>
</body>
</html>
